﻿Public Class DRUGAGENT
    Dim connect As CONDB = CONDB.NewConnection()
    Dim datejaja As New FriendsDate()
    'Dim connectlocal As conlocal = conlocal.NewConnection

    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub DRUGAGENT_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        selectDrugAgentGrid()

        'combobox
        Dim taxType = New Dictionary(Of String, String)()
        taxType("1") = "Without Tax"
        taxType("2") = "Include"
        taxType("3") = "Exclude"
        taxTypeCmb.DataSource = New BindingSource(taxType, Nothing)
        taxTypeCmb.DisplayMember = "Value"
        taxTypeCmb.ValueMember = "Key"

        DesignForm(Me)
        DesignGrid(agentGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    'if select no tax, set taxrate = 0
    Private Sub taxTypeCmb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles taxTypeCmb.SelectedIndexChanged
        If taxTypeCmb.SelectedIndex = 0 Then
            taxRateTxt.Text = 0
        End If
    End Sub

    'Private Sub taxTypeCmb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles taxTypeCmb.SelectedIndexChanged
    '    MsgBox(taxTypeCmb.SelectedValue.ToString)
    'End Sub

    Public Sub selectDrugAgentGrid()
        Dim objSelectDrugAgent As New DRUGAGENTCLASS
        Dim DRUGAGENT As DataTable
        datejaja.culTure_TH()
        datejaja.TO_TH()
        DRUGAGENT = connect.GetTable(objSelectDrugAgent.selectAgent_)
        agentGrid.PrimaryGrid.DataSource = DRUGAGENT
    End Sub

    Public Sub clearForm()
        AGENTCODETxt.Text = ""
        COMPANY_NAMETxt.Text = ""
        CONTACT_NAMETxt.Text = ""
        ADDRESSTxt.Text = ""
        POSTCODETxt.Text = ""
        TELTxt.Text = ""
        MTELTxt.Text = ""
        EMAILTxt.Text = ""
        FAXTxt.Text = ""
        IDBANKTxt.Text = ""
        LIMITAMOUNTTxt.Text = ""
        CREDITTERMTxt.Text = ""
        LEADTIMETxt.Text = ""
        agentStatusChk.Checked = False
        taxTypeCmb.SelectedValue = -1
        taxRateTxt.Text = ""
        TaxIDTxt.Clear()
    End Sub

    Private Sub DrugUseTemplateGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles agentGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow

        Try
            AGENTCODETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสผู้ค้า").Value()
        Catch ex As Exception
            AGENTCODETxt.Text = ""
        End Try

        Try
            COMPANY_NAMETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อผู้ค้า").Value()
        Catch ex As Exception
            COMPANY_NAMETxt.Text = ""
        End Try

        Try
            CONTACT_NAMETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อตัวแทน").Value()
        Catch ex As Exception
            CONTACT_NAMETxt.Text = ""
        End Try

        Try
            ADDRESSTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ที่อยู่").Value()
        Catch ex As Exception
            ADDRESSTxt.Text = ""
        End Try

        Try
            POSTCODETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสไปรษณีย์").Value()
        Catch ex As Exception
            POSTCODETxt.Text = ""
        End Try

        Try
            TELTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("โทรศัพท์").Value()
        Catch ex As Exception
            TELTxt.Text = ""
        End Try

        Try
            MTELTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("โทรศัพท์มือถือ").Value()
        Catch ex As Exception
            MTELTxt.Text = ""
        End Try

        Try
            EMAILTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("E-mail").Value()
        Catch ex As Exception
            EMAILTxt.Text = ""
        End Try

        Try
            FAXTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("แฟ็กซ์").Value()
        Catch ex As Exception
            FAXTxt.Text = ""
        End Try

        Try
            IDBANKTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("บัญชีธนาคาร").Value()
        Catch ex As Exception
            IDBANKTxt.Text = ""
        End Try

        Try
            LIMITAMOUNTTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("วงเงิน").Value()
        Catch ex As Exception
            LIMITAMOUNTTxt.Text = ""
        End Try

        Try
            CREDITTERMTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เครดิต").Value()
        Catch ex As Exception
            CREDITTERMTxt.Text = ""
        End Try

        Try
            LEADTIMETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ระยะเวลาส่งของ").Value()
        Catch ex As Exception
            LEADTIMETxt.Text = ""
        End Try

        Try
            'MsgBox(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("TAX").Value())
            If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("TAX").Value() = "Without Tax" Then
                taxTypeCmb.SelectedIndex = 0
            ElseIf CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("TAX").Value() = "Include" Then
                taxTypeCmb.SelectedIndex = 1
            ElseIf CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("TAX").Value() = "Exclude" Then
                taxTypeCmb.SelectedIndex = 2
            ElseIf CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("TAX").Value() = "Unknown" Then
                taxTypeCmb.SelectedIndex = -1
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
            taxTypeCmb.SelectedIndex = -1
        End Try

        Try
            taxRateTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("อัตราภาษี").Value()
        Catch ex As Exception
            taxRateTxt.Text = ""
        End Try

        Try
            TaxIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เลขที่ผู้เสียภาษี").Value()
        Catch ex As Exception
            TaxIDTxt.Clear()
        End Try

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            agentStatusChk.Checked = True
        Else
            agentStatusChk.Checked = False
        End If

        Dim objChkboxUpdate As New CLICKGRIDCHECKBOX
        objChkboxUpdate.clickChkboxUpdate("agent", CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value(), "STATUS", "AGENTCODE", AGENTCODETxt.Text)
    End Sub

    Private Sub addCountry_Click(sender As Object, e As EventArgs) Handles addAgent.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If AGENTCODETxt.Text.ToString.Length > 0 And COMPANY_NAMETxt.Text.ToString.Length > 0 Then
                    'connectlocal = conlocal.NewConnection
                    Dim objInsertData As New DRUGAGENTCLASS
                    objInsertData.AGENTCODE_ = AGENTCODETxt.Text
                    objInsertData.COMPANY_NAME_ = COMPANY_NAMETxt.Text
                    objInsertData.CONTACT_NAME_ = CONTACT_NAMETxt.Text
                    objInsertData.ADDRESS_ = ADDRESSTxt.Text
                    objInsertData.POSTCODE_ = POSTCODETxt.Text
                    objInsertData.TEL_ = TELTxt.Text
                    objInsertData.MTEL_ = MTELTxt.Text
                    objInsertData.EMAIL_ = EMAILTxt.Text
                    objInsertData.FAX_ = FAXTxt.Text
                    objInsertData.IDBANK_ = IDBANKTxt.Text
                    objInsertData.LIMITAMOUNT_ = LIMITAMOUNTTxt.Text
                    objInsertData.CREDITTERM_ = CREDITTERMTxt.Text
                    objInsertData.LEADTIME_ = LEADTIMETxt.Text
                    objInsertData.STATUS_ = agentStatusChk.Checked
                    objInsertData.TAX_ = taxTypeCmb.SelectedValue
                    objInsertData.TAXRATE_ = taxRateTxt.Text
                    objInsertData.TAXID_ = TaxIDTxt.Text

                    If objInsertData.insertAgent() = True Then
                        selectDrugAgentGrid()
                        clearForm()
                    End If

                    connect.Dispose()
                Else
                    MsgBox("ท่านต้องทำการกรอกข้อมูลให้ครบถ้วนทุกช่องก่อนทำการเพิ่มข้อมูล")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editCountry_Click(sender As Object, e As EventArgs) Handles editAgent.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If AGENTCODETxt.Text.ToString.Length > 0 And COMPANY_NAMETxt.Text.ToString.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลผู้ค้ายาและเวชภัณฑ์ใช่หรือไม่", MsgBoxStyle.OkCancel)

                    If editStat = 1 Then
                        'connectlocal = conlocal.NewConnection
                        Dim objEditData As New DRUGAGENTCLASS
                        objEditData.AGENTCODE_ = AGENTCODETxt.Text
                        objEditData.COMPANY_NAME_ = COMPANY_NAMETxt.Text
                        objEditData.CONTACT_NAME_ = CONTACT_NAMETxt.Text
                        objEditData.ADDRESS_ = ADDRESSTxt.Text
                        objEditData.POSTCODE_ = POSTCODETxt.Text
                        objEditData.TEL_ = TELTxt.Text
                        objEditData.MTEL_ = MTELTxt.Text
                        objEditData.EMAIL_ = EMAILTxt.Text
                        objEditData.FAX_ = FAXTxt.Text
                        objEditData.IDBANK_ = IDBANKTxt.Text
                        objEditData.LIMITAMOUNT_ = LIMITAMOUNTTxt.Text
                        objEditData.CREDITTERM_ = CREDITTERMTxt.Text
                        objEditData.LEADTIME_ = LEADTIMETxt.Text
                        objEditData.STATUS_ = agentStatusChk.Checked
                        objEditData.TAX_ = taxTypeCmb.SelectedValue
                        objEditData.TAXRATE_ = taxRateTxt.Text
                        objEditData.TAXID_ = TaxIDTxt.Text

                        If objEditData.editAgent() = True Then
                            selectDrugAgentGrid()
                            clearForm()
                        End If

                        connect.Dispose()
                    End If
                Else
                    MsgBox("ท่านต้องทำการกรอกข้อมูลให้ครบถ้วนก่อนทำการแก้ไขข้อมูล")
                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL("agent")
        nextform.ShowDialog()
    End Sub

    Private Sub exportExcel_Click(sender As Object, e As EventArgs) Handles exportExcel.Click
        Dim nextform As New IMPORTEXCEL("agent")
        nextform.ShowDialog()
    End Sub
End Class